package com.fivefu.core.hik.utils;

import com.fivefu.base.common.utils.str.StrUtils;
import com.github.xiaoymin.knife4j.core.util.StrUtil;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

public class ISODateUtils {

    private static String DAFAULT_FORMATE_DATETIME = "uuuu-MM-dd'T'HH:mm:ss.SSSxxx";//2004-05-03T17:30:08.000+08:00

    private static String FORMATE_DATETIME = "uuuu-MM-dd'T'HH:mm:ssxxx";//2004-05-03T17:30:08+08:00

    private static String FORMATE_DATETIME2 = "uuuuMMdd'T'HHmmssx";//20040503T173008+08


    public static String formatLocalDateTimeToISODate(LocalDateTime localDateTime){
        String time = formatLocalDateTimeToISODate(localDateTime, DAFAULT_FORMATE_DATETIME);
        return time;
    }

    public static String formatLocalDateTimeToISODate(LocalDateTime localDateTime, String formatStr){
        if (StrUtils.isNull(formatStr)) formatStr = DAFAULT_FORMATE_DATETIME;
        if (localDateTime != null) {
            String time = OffsetDateTime.of(localDateTime, ZoneOffset.ofHours(8))
                    .truncatedTo(ChronoUnit.MILLIS)
                    .format(
                            java.time.format.DateTimeFormatter.ofPattern(formatStr)
                    );
            return time;
        }
        return null;
    }

    public static void main(String[] args) {
        LocalDateTime time = LocalDateTime.now();
        time = LocalDateTime.of(2004,5,3,17,30,8);

        String time1 = formatLocalDateTimeToISODate(time);
        System.out.println(time1);

        time1 = formatLocalDateTimeToISODate(time, FORMATE_DATETIME);
        System.out.println(time1);

        time1 = formatLocalDateTimeToISODate(time, FORMATE_DATETIME2);
        System.out.println(time1);
    }

}
